package com.zito.pojo;

import java.util.List;


import com.downlove.common.DateUtils;
import com.downlove.common.LinkTool;
import com.downlove.common.Storage.ImageInfo;
import com.downlove.database.POJO;
import com.downlove.database.QueryHelper;
import com.downlove.database.SequenceManager;
import com.zito.bean.Articlephotos;

public class ArticlephotosPOJO extends POJO {

	/**
	 * 
	 */
	private static final long serialVersionUID = -4164489013007969556L;

	/**
	 * 直接从ImageInfo中获取上传图片的信息保存
	 * 
	 * @param info
	 * @return
	 */
	public static Long fromFileinfo(ImageInfo info,String identifies) {
		if (info == null)
			return -1L;
		Articlephotos photos = new Articlephotos();
		photos.setArticleid(-1L);// 临时附值
		photos.setFileservername(info.getImageName());
		photos.setFileserverpath(LinkTool.images(info.getImagePath()));
		photos.setIdentifies(identifies);// 临时附值
		photos.setOriginalname(info.getImageOriginalName());
		photos.setThumbname(info.getImageThumbName());
		photos.setThumbpath(LinkTool.images(info.getImageThumbPath()));
		photos.setUploadtime(DateUtils.getDateTime());
		photos.setId(SequenceManager.nextID(Articlephotos.class.getSimpleName()));
		return photos.Save();
	}

	/**
	 * 保存文章后将对应图片文件的文章ID绑定所需要的sql
	 * @return
	 */
	public String updateObject() {
		return "UPDATE " + TableName() + " SET articleid=? WHERE identifies=? AND articleid=-1";
	}
	
	@SuppressWarnings("unchecked")
	public List<? extends POJO> forArticleList(long articleid) {
		String sql = "SELECT id FROM " + TableName() + " WHERE articleid="+articleid+"  ORDER BY id";
		List<Long> tmpList = QueryHelper.queryList(sql, 1, TotalCount());
		return this.LoadList(tmpList);
	}
	
	public String inIdList(String list) {
		String sql = "SELECT id as ids FROM " + TableName()
				+ " WHERE articleid in (" + list + ")";
		List<Long> tmplist = QueryHelper.queryList(sql);
		StringBuilder string = new StringBuilder("");
		for (int i = 0; i < tmplist.size(); i++){
			if (i > 0)
				string.append(',');
			string.append(tmplist.get(i));
		}
		//System.out.println("string : " + string.toString());
		return string.toString();
	}

}
